package com.crud.project.controller;

import com.crud.project.po.Permission;
import com.crud.project.po.Role;
import com.crud.project.po.User;
import com.crud.project.service.PermissionService;
import com.crud.project.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;
    @GetMapping("/role")
    public String findAllRole(@RequestParam(required=true,defaultValue="1") Integer page, Model model){
        PageHelper.startPage(page,3);
        List<Role>roleList=roleService.findAll();
        PageInfo<Role> pageInfo=new PageInfo<>(roleList);
        model.addAttribute("role",pageInfo);
        return "role";
    }

    @GetMapping("/role-input")
    public String roleinput(Model model, HttpSession httpSession){
        if (httpSession.getAttribute("role")==null){
            Role role=new Role();
            role.setPermissionList(permissionService.findPermissionList());
            model.addAttribute("role",role);
        }
        return "Role-input";
    }

    @GetMapping("/updateRole/{id}")
    public String updateInput(@PathVariable Long id,Model model){
        Role role=roleService.findRoleById(id);
//        String tagids= ""+roleService.findPermissionId(Long.parseLong(role.getRole_id()));
        List<String> list=roleService.findPermissionId(Long.parseLong(role.getRole_id()));
        String tagids=StringUtils.join(list,",");
        role.setTagIds(tagids);
        role.setPermissionList(permissionService.findPermissionList());
        model.addAttribute("role",role);
        return "Role-input";
    }
    @PostMapping("/updateRole/{id}")
    public String updateRole(@PathVariable Long id,Role role,Model model){
        Role role1=roleService.findRoleById(id);
        role.setRole_id(role1.getRole_id());
        BeanUtils.copyProperties(role,role1);
//        List<String> list=permissionService.findPermissionListByRoleId(Long.parseLong(role1.getRole_id()));
        permissionService.deletePermissionByRoleId(Long.parseLong(role1.getRole_id()));
       String[]ids=role1.getTagIds().split(",");
       for (int i=0;i<ids.length;i++){
           permissionService.insertPermissionById(Long.parseLong(role1.getRole_id()),Long.parseLong(ids[i]));
       }
        roleService.updateRole(role1);
        return "redirect:/role";
    }

    @PostMapping("/insertRole")
    public String insertRole(Role role){
        roleService.insertRole(role);
        String ids=role.getTagIds();
        Long role_id=Long.parseLong(role.getRole_id());
        List<Long> list = new ArrayList<>();
        if (!"".equals(ids) && ids != null) {
            String[] idarray = ids.split(",");
            for (int i=0; i < idarray.length;i++) {
                list.add(new Long(idarray[i]));
            }
        }
        for (Long permission_id:list){
            permissionService.insertPermissionById(role_id,permission_id);
        }
        return "redirect:/role";
    }


    @GetMapping("/deleteRole/{id}")
    public String deleteById(@PathVariable Long id){
        permissionService.deletePermissionByRoleId(id);
        roleService.deleteRole(id);
        return "redirect:/role";
    }
}
